﻿
function odometersWindow() {
    var day = new Date();
    var today = day.getFullYear() + '-' + padLeft((day.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(day.getDate().toString(), "0", 2);
    var yyday = addDays(day, -1);
    var yestoday = yyday.getFullYear() + '-' + padLeft((yyday.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(yyday.getDate().toString(), "0", 2);

    var trackerstore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'HandlerSearchTrackerName',
            timeout: 240000
        }),
        reader: new Ext.data.JsonReader({
            fields: [{ name: 'trackname'}],
            root: "data"
        })
    });

    trackerstore.load();

    var trackNameCombo = new Ext.ux.form.CheckboxCombo({
        store: trackerstore,
        mode: 'local',
        displayField: 'trackname',
        width: 200,
        valueField: 'trackname',
        enableKeyEvents: true,
        editable: true
    });

    trackNameCombo.on('keyup', function() {
        trackerstore.filter('trackname', trackNameCombo.getRawValue(), true, false);
        trackNameCombo.bindStore(trackerstore);
    });

    trackNameCombo.on('blur', function() {
        trackerstore.clearFilter();
        trackNameCombo.bindStore(trackerstore);
    });


    var fromDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today
    });


    var fromTimeEditor = new Ext.form.TimeField({
        value:"00:00",
        format: 'H:i',
        width: 70
    });

    var toDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today
    });

    var toTimeEditor = new Ext.form.TimeField({
        value:'23:59',
        format: 'H:i',
        width: 70
    });

    var fields = ["trackername", "sssid", "begintime", { name: "beginodometers", type: "integer" }, "endtime", { name: "endodometers", type: "integer" }, { name: "odo", type: "integer"}];
    

    var odostore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchOdometers",
            method: "POST",
            timeout: 120000
        }),
        reader: new Ext.data.JsonReader({
            fields: fields,
            root: "data",
            totalProperty: "totalCount"
        })
    });

    odostore.addListener('load', function(store, records) {
        var i;
        for (i = 0; i < records.length; i++) {
            var endodo = (records[i].get("endodometers") / 1000).toFixed(2);
            var beginodo = (records[i].get("beginodometers") / 1000).toFixed(2);
            var odo = endodo - beginodo;
            if (showmiles) {
                endodo = kilos2miles(endodo);
                beginodo = kilos2miles(beginodo);
                odo = kilos2miles(odo);
            }
            records[i].set("beginodometers", beginodo);
            records[i].set("endodometers", endodo);
            records[i].set("odo", odo);
            
        }
    });

    var showTypeEditor = new Ext.form.ComboBox({
        editable: false,
        disableKeyFilter: true,
        typeAhead: false,
        mode: 'local',
        forceSelection: true,
        triggerAction: 'all',
        store: new Ext.data.ArrayStore({
            fields: ['cmdid', 'text'],
            data: [['0', _Ext_List], ['1', _Ext_BarChart]]
        }),
        displayField: 'text',
        width: 100,
        valueField: 'cmdid'
    });

    showTypeEditor.setValue("0");

    var tbar = new Ext.Toolbar({
        items: [_Alarm_TrackerName + ':',
        trackNameCombo,
        {
            xtype: 'tbseparator', width: 5
        },
        _Alarm_FromTime + ':',
        fromDateEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        fromTimeEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        _Alarm_ToTime + ':',
        toDateEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        toTimeEditor,
        {
            xtype: 'tbseparator', width: 5
        }, showTypeEditor, {
            tooltip: { text: _TrackerMain_Search, autoHide: true },
            icon: 'Content/images/search16.gif',
            handler: function() {
                //odowin.removeAll();
                //odowin.add(barchart);
                if (showTypeEditor.getValue() == "1") {
                    grid.setVisible(false);
                    barchart.setVisible(true);
                }
                else {
                    //barchart.setVisible(false);
                    grid.setVisible(true);
                }
                odostore.reload({
                    params: {
                        trackName: trackNameCombo.getRawValue(),
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value

                    }
                });

            }
        }, {
            tooltip: { text: _TrackerMain_ExportToExcel, autoHide: true },
            icon: 'Content/images/excel16.gif',
            handler: function() {

                if (!Ext.fly('totaldistanceexcel')) {
                    var frm = document.createElement('form');
                    frm.id = 'OdometerStatisticExcel';
                    frm.name = id;
                    frm.style.display = 'none';
                    document.body.appendChild(frm);
                }
                Ext.Ajax.request({
                    url: 'HandlerExcel',
                    form: Ext.fly('OdometerStatisticExcel'),
                    method: 'POST',
                    params: {
                        exceltype: 'totaldistanceexcel',
                        start: 0,
                        limit: 60000,
                        trackName: trackNameCombo.getRawValue(),
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value
                    },
                    isUpload: true
                });

            }
}]
        });

    var headerBODO = _ODOReport_BeginOdometers + (showmiles ? "(" + _Ext_mile + ")" : "(" + _Ext_km + ")");
    var headerEODO = _ODOReport_EndOdometers + (showmiles ? "(" + _Ext_mile + ")" : "(" + _Ext_km + ")");
    var headerJourney = showmiles ? _TrackerMain_JourneyMiles : _TrackerMain_JouneryKM;
    var cm = [
            { header: _Alarm_Header_Name, width: 85, sortable: true, menuDisabled: true, dataIndex: 'trackername' },
            { header: _ODOReport_BeginTime, width: 120, sortable: true, menuDisabled: true, dataIndex: 'begintime'},
            { header: headerBODO, width: 130, sortable: true, menuDisabled: true, dataIndex: 'beginodometers' },
            { header: _ODOReport_EndTime, width: 120, sortable: true, menuDisabled: true, dataIndex: 'endtime' },
            { header: headerEODO, width: 130, sortable: true, menuDisabled: true, dataIndex: 'endodometers' },
            { header: headerJourney, width: 85, sortable: true, menuDisabled: true, dataIndex: 'odo' }
        ];
   

    var grid = new Ext.grid.GridPanel({
        store: odostore,
        loadMask: true,
        title: _ODOReport_Title,
        columns: cm,
        width: 805,
        height: 420,
        constrain:true,
        
        minimizable: true,
         listeners: {
            'minimize': function() {
                addTaskButtonForWindow(win);
            }
        }
    });

    var barchart = new Ext.chart.ColumnChart({
        store: odostore,
        xField: 'trackername',
        yField: 'odo'
    }); 
    
    grid.addListener("rowdblclick", function(grid, index) {
       

    });

    var taskButton;
    var odowin = new Ext.Window({
        title: _ODOReport_Title,
        layout: 'fit',
        height: 490,
        width: 1000,
        items: [grid,barchart],
        tbar: tbar,
        //iconCls: 'report-history',
        closeAction: 'close',
        minimizable: true,
//        bbar: new Ext.PagingToolbar({
//            //pageSize: pagesize,
//            store: odostore,
//            beforePageText: _Alarm_Page,
//            refreshTex: _Alarm_Refresh,
//            displayMsg: _Alarm_Displaying,
//            emptyMsg: _Alarm_Alarm_emptyMsg,
//            displayInfo: true
//        }),
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(odowin);
            }
        }

    });
    odostore.on('beforeload', function(store) {
    odostore.baseParams = {
            trackName: trackNameCombo.getValue(),
            fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
            totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value
            }
        })
        grid.setVisible(false);
        barchart.setVisible(false);
    odowin.show();
};